package com.intct.hbase.dml;

import org.apache.hadoop.hbase.Cell;
import org.apache.hadoop.hbase.CellUtil;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.*;
import org.apache.hadoop.hbase.util.Bytes;

import java.io.IOException;

/**
 * @author gufg
 * @since 2025-08-06 15:23
 */
public class GetCommand {
    public static void main(String[] args) throws IOException {
        // 创建HBase连接
        Connection connection = ConnectionFactory.createConnection();

        // 声明TableName实例
        TableName tableName = TableName.valueOf("intct83", "t5");

        // 获取Admin实例
        Admin admin = connection.getAdmin();
        Table table = null;

        try {
            // 判断表存在
            if (admin.tableExists(tableName)) {
                // 获取Table实例
                table = connection.getTable(tableName);

                Get get = new Get("1001".getBytes());

                // 执行查询--Get
                Result result = table.get(get);

                Cell[] cells = result.rawCells();
                for (Cell cell : cells) {
                    // Cell == rowkey +  列族 + 列限定符 + 值
                    String rowkey = Bytes.toString(CellUtil.cloneRow(cell));
                    String family = Bytes.toString(CellUtil.cloneFamily(cell));
                    String qualifier = Bytes.toString(CellUtil.cloneQualifier(cell));
                    String value = Bytes.toString(CellUtil.cloneValue(cell));
                    System.out.println("rowkey = " + rowkey + "; family = " + family + "; qualifier = " + qualifier + "; value = " + value);
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (table != null) {
                table.close();
            }
            if (admin != null) {
                admin.close();
            }
            if (connection != null) {
                connection.close();
            }
        }

    }
}
